#ifndef unigame_Timer_h
#define unigame_Timer_h

#include <list>
#include "config/Def.h"

namespace unigame
{
    class TimerEvent;
    class ITimerCallBack;
    class Timer
    {
    public:
        ~Timer();
        
        void update();
        
        static Timer* instance()
        {
            if(!s_instance)
            {
                s_instance = uni_new Timer();
            }
            return s_instance;
        }
        
        static void destroy()
        {
            uni_delete(s_instance);
        }

        float removeRealTimeTimer(ITimerCallBack* callBack);
        float removeTimer(ITimerCallBack* callBack);
        float removeRealTimeTimerById(ITimerCallBack* callBack, int32u iden);
        float removeTimerById(ITimerCallBack* callBack, int32u iden);
        void removeAllRealTimeTimerOf(ITimerCallBack* callBack);
        void removeAllTimerOf(ITimerCallBack* callBack);
        
        void registerTimer(ITimerCallBack* callBack, float time, float loop = -1, int32u iden = 0);
        void registerRealTimeTimer(ITimerCallBack* callBack, float time, float loop = -1, int32u iden = 0);
        
    private:
        Timer();
        
        void checkTimeEvent(std::list<TimerEvent*>& list, int64u time);
        float removeTimerById(ITimerCallBack* callBack, int32u iden, std::list<TimerEvent*>& list, int64u time);
        float removeTimer(ITimerCallBack* callBack, std::list<TimerEvent*>& list, int64u time);
        void removeAllTimerOf(ITimerCallBack* callBack, std::list<TimerEvent*>& list);
        void registerTimer(TimerEvent* event, std::list<TimerEvent*>& list, int64u time);
        
    private:
        std::list<TimerEvent*> m_realTimeList;
        std::list<TimerEvent*> m_list;
        int64u m_inGameTime;
        
        static Timer* s_instance;
    };
}

#endif
